#include "raylib.h"
#include <cmath>
#include <string>

// 坐标系转换函数：将数学坐标转换为屏幕坐标
Vector2 MathToScreen(float x, float y, int centerX, int centerY, float scale) {
    return (Vector2){ centerX + x * scale, centerY - y * scale };
}

// 在 MathToScreen 函数后添加 CodepointRemoveDuplicates 函数
static int *CodepointRemoveDuplicates(int *codepoints, int codepointCount, int *codepointsResultCount)
{
    int codepointsNoDupsCount = codepointCount;
    int *codepointsNoDups = (int *)calloc(codepointCount, sizeof(int));
    memcpy(codepointsNoDups, codepoints, codepointCount*sizeof(int));

    // Remove duplicates
    for (int i = 0; i < codepointsNoDupsCount; i++)
    {
        for (int j = i + 1; j < codepointsNoDupsCount; j++)
        {
            if (codepointsNoDups[i] == codepointsNoDups[j])
            {
                for (int k = j; k < codepointsNoDupsCount; k++) codepointsNoDups[k] = codepointsNoDups[k + 1];

                codepointsNoDupsCount--;
                j--;
            }
        }
    }

    *codepointsResultCount = codepointsNoDupsCount;
    return codepointsNoDups;
}

int main(void)
{
    const int screenWidth = 800;
    const int screenHeight = 600;
    InitWindow(screenWidth, screenHeight, "y = ax² + bx + c 函数图像");
    
    // 修改字体加载部分，添加所有可能的rootInfo文本
    const char* helpText = "使用方向键调整参数：\n"
                         "Q/A - 调整二次项系数a\n"
                         "W/S - 调整一次项系数b\n"
                         "E/D - 调整常数项c\n"
                         "SPACE - 重置参数";
    const char* allTexts = "使用方向键调整参数：\n"
                         "Q/A - 调整二次项系数a\n"
                         "W/S - 调整一次项系数b\n"
                         "E/D - 调整常数项c\n"
                         "SPACE - 重置参数\n"
                         "函数与x轴有两个交点\n"
                         "函数与x轴有一个交点（相切）\n"
                         "函数与x轴没有交点";
                         
    int codepointCount = 0;
    int* codepoints = LoadCodepoints(allTexts, &codepointCount);  // 使用包含所有文本的字符串
    int codepointsNoDupsCount = 0;
    int* codepointsNoDups = CodepointRemoveDuplicates(codepoints, codepointCount, &codepointsNoDupsCount);
    UnloadCodepoints(codepoints);
    
    Font font = LoadFontEx("resources/msyh.ttf", 20, codepointsNoDups, codepointsNoDupsCount);
    free(codepointsNoDups);
    SetTextureFilter(font.texture, TEXTURE_FILTER_BILINEAR);
    
    SetTargetFPS(60);

    // 坐标系参数
    const int centerX = screenWidth / 2;
    const int centerY = screenHeight / 2;
    const float scale = 50.0f;
    
    // 函数参数 y = ax² + bx + c
    const float default_a = 1.0f;    // 默认二次项系数
    const float default_b = 0.0f;    // 默认一次项系数
    const float default_c = 0.0f;    // 默认常数项
    float a = default_a;
    float b = default_b;
    float c = default_c;
    const float change_rate = 0.02f;
    
    while (!WindowShouldClose())
    {
        // 更新参数
        if (IsKeyPressed(KEY_SPACE)) {  // 按空格键重置所有系数
            a = default_a;
            b = default_b;
            c = default_c;
        }
        
        if (IsKeyDown(KEY_Q)) a += change_rate;
        if (IsKeyDown(KEY_A)) a -= change_rate;
        if (IsKeyDown(KEY_W)) b += change_rate;
        if (IsKeyDown(KEY_S)) b -= change_rate;
        if (IsKeyDown(KEY_E)) c += change_rate;
        if (IsKeyDown(KEY_D)) c -= change_rate;

        // 计算判别式
        float delta = b * b - 4 * a * c;

        BeginDrawing();
        ClearBackground(RAYWHITE);

        // 绘制坐标轴
        DrawLine(0, centerY, screenWidth, centerY, BLACK);
        DrawLine(centerX, 0, centerX, screenHeight, BLACK);
        
        // 绘制刻度
        for (int i = -10; i <= 10; i++) {
            Vector2 tickX = MathToScreen(i, 0, centerX, centerY, scale);
            Vector2 tickY = MathToScreen(0, i, centerX, centerY, scale);
            DrawLine(tickX.x, tickX.y - 5, tickX.x, tickX.y + 5, BLACK);
            DrawLine(tickY.x - 5, tickY.y, tickY.x + 5, tickY.y, BLACK);
        }

        // 绘制二次函数曲线
        for (float x = -10; x <= 10; x += 0.05f) {
            float y = a * x * x + b * x + c;
            Vector2 point = MathToScreen(x, y, centerX, centerY, scale);
            DrawCircle(point.x, point.y, 1.5f, RED);
        }

        // 显示函数表达式和判别式
        const char* expression = TextFormat("y = %.1fx² + %.1fx + %.1f", a, b, c);
        const char* discriminant = TextFormat("b² - 4ac = %.2f", delta);
        const char* rootInfo;
        if (delta > 0) {
            rootInfo = "函数与x轴有两个交点";
        } else if (delta == 0) {
            rootInfo = "函数与x轴有一个交点（相切）";
        } else {
            rootInfo = "函数与x轴没有交点";
        }

        // 显示函数表达式、判别式和说明文本
        DrawText(expression, 10, 10, 20, DARKGRAY);
        DrawText(discriminant, 10, 35, 20, DARKGRAY);
        DrawTextEx(font, rootInfo, (Vector2){10, 60}, 20, 1, DARKGRAY);
        DrawTextEx(font, helpText, (Vector2){10, 90}, 20, 1, DARKGRAY);  // 只显示帮助文本

        EndDrawing();
    }

    UnloadFont(font);
    CloseWindow();
    return 0;
}
